<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License.  You may obtain a copy of the License at
  ~
  ~   http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied.  See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
-->

<app-help-message-show
  [help_message_content]="'alert.help.converge' | i18n"
  [guild_link]="'alert.help.converge.link' | i18n"
  [module_name]="'menu.alert.converge'"
  [icon_name]="'filter'"
></app-help-message-show>

<nz-divider></nz-divider>

<app-toolbar>
  <ng-template #left>
    <button nz-button nzType="primary" (click)="sync()" nz-tooltip [nzTooltipTitle]="'common.refresh' | i18n">
      <i nz-icon nzType="sync" nzTheme="outline"></i>
    </button>
    <button nz-button nzType="primary" (click)="onNewAlertConverge()">
      <i nz-icon nzType="appstore-add" nzTheme="outline"></i>
      {{ 'alert.converge.new' | i18n }}
    </button>
    <button nz-button nzType="primary" nzDanger (click)="onDeleteAlertConverges()">
      <i nz-icon nzType="delete" nzTheme="outline"></i>
      {{ 'alert.converge.delete' | i18n }}
    </button>
  </ng-template>
  <ng-template #right>
    <app-multi-func-input
      groupStyle="width: 250px;"
      [placeholder]="'alert.converge.name' | i18n"
      [(value)]="search"
      (valueChange)="loadAlertConvergeTable()"
    />
  </ng-template>
</app-toolbar>

<nz-table
  #fixedTable
  [nzData]="converges"
  [nzPageIndex]="pageIndex"
  [nzPageSize]="pageSize"
  [nzTotal]="total"
  nzFrontPagination="false"
  [nzLoading]="tableLoading"
  nzShowSizeChanger
  [nzShowTotal]="rangeTemplate"
  [nzPageSizeOptions]="[8, 15, 25]"
  (nzQueryParams)="onTablePageChange($event)"
  nzShowPagination="true"
  [nzScroll]="{ x: '1240px' }"
>
  <thead>
    <tr>
      <th nzAlign="center" nzLeft nzWidth="3%" [(nzChecked)]="checkedAll" (nzCheckedChange)="onAllChecked($event)"></th>
      <th nzAlign="center" nzWidth="16%">{{ 'alert.converge.name' | i18n }}</th>
      <th nzAlign="center" nzWidth="18%">{{ 'alert.converge.tags' | i18n }}</th>
      <th nzAlign="center" nzWidth="15%">{{ 'alert.converge.eval-interval' | i18n }}</th>
      <th nzAlign="center" nzWidth="15%">{{ 'alert.converge.enable' | i18n }}</th>
      <th nzAlign="center" nzWidth="16%">{{ 'common.edit-time' | i18n }}</th>
      <th nzAlign="center" nzWidth="17%">{{ 'common.edit' | i18n }}</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let data of fixedTable.data">
      <td nzAlign="center" nzLeft [nzChecked]="checkedConvergeIds.has(data.id)" (nzCheckedChange)="onItemChecked(data.id, $event)"></td>
      <td nzAlign="center">
        <span> {{ data.name }} </span>
      </td>
      <td nzAlign="center" *ngIf="!data.matchAll">
        <nz-tag nzColor="processing" style="margin-top: 2px" *ngFor="let tag of data.tags; let i = index">
          {{ sliceTagName(tag) }}
        </nz-tag>
      </td>
      <td nzAlign="center" *ngIf="data.matchAll">
        <nz-tag nzColor="warning">
          {{ 'alert.converge.match-all' | i18n }}
        </nz-tag>
      </td>
      <td nzAlign="center">
        <nz-tag nzColor="processing">
          <i nz-icon nzType="clock-circle" nzTheme="outline"></i>
          <span>{{ data.evalInterval == undefined ? 0 : data.evalInterval }}</span>
        </nz-tag>
      </td>
      <td nzAlign="center">
        <nz-switch [(ngModel)]="data.enable" (ngModelChange)="updateAlertConverge(data)" name="enable"></nz-switch>
      </td>
      <td nzAlign="center">{{ (data.gmtUpdate ? data.gmtUpdate : data.gmtCreate) | date : 'YYYY-MM-dd HH:mm:ss' }}</td>
      <td nzAlign="center">
        <div class="actions">
          <button
            nz-button
            nzType="primary"
            (click)="onEditAlertConverge(data.id)"
            nz-tooltip
            [nzTooltipTitle]="'alert.converge.edit' | i18n"
          >
            <i nz-icon nzType="edit" nzTheme="outline"></i>
          </button>
          <button
            nz-button
            nzDanger
            nzType="primary"
            (click)="onDeleteOneAlertConverge(data.id)"
            nz-tooltip
            [nzTooltipTitle]="'alert.converge.delete' | i18n"
          >
            <i nz-icon nzType="delete" nzTheme="outline"></i>
          </button>
        </div>
      </td>
    </tr>
  </tbody>
</nz-table>

<ng-template #rangeTemplate> {{ 'common.total' | i18n }} {{ total }} </ng-template>

<!-- new or update alert-converge pop-up box -->
<nz-modal
  [(nzVisible)]="isManageModalVisible"
  [nzTitle]="isManageModalAdd ? ('alert.converge.new' | i18n) : ('alert.converge.edit' | i18n)"
  (nzOnCancel)="onManageModalCancel()"
  (nzOnOk)="onManageModalOk()"
  nzMaskClosable="false"
  nzWidth="40%"
  [nzOkLoading]="isManageModalOkLoading"
>
  <div *nzModalContent class="-inner-content">
    <form nz-form #ruleForm="ngForm">
      <nz-form-item>
        <nz-form-label [nzSpan]="10" nzFor="rule_name" nzRequired="true">{{ 'alert.converge.name' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="8" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="converge.name" nz-input required name="rule_name" type="text" id="rule_name" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label nzSpan="10" nzRequired="true" nzFor="matchAll">{{ 'alert.converge.match-all' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="8">
          <nz-switch [(ngModel)]="converge.matchAll" name="matchAll" id="matchAll"></nz-switch>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="!converge.matchAll">
        <nz-form-label nzSpan="10" nzFor="tags">{{ 'alert.converge.tags' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="8">
          <nz-select
            [(ngModel)]="matchTags"
            [nzDisabled]="converge.matchAll"
            (nzOpenChange)="loadTagsOption()"
            [nzOptions]="tagsOption"
            [nzMaxTagCount]="5"
            [nzDropdownMatchSelectWidth]="false"
            nzShowSearch
            nzMode="multiple"
            nzAllowClear
            [nzPlaceHolder]="'alert.notice.rule.tag.placeholder' | i18n"
            required
            name="tags"
            id="tags"
          >
          </nz-select>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="!converge.matchAll">
        <nz-form-label nzSpan="10" nzFor="priorities">{{ 'alert.converge.priority' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="8">
          <nz-select
            [(ngModel)]="converge.priorities"
            [nzDisabled]="converge.matchAll"
            (ngModelChange)="onPrioritiesChange()"
            nzShowSearch
            nzMode="multiple"
            nzAllowClear
            [nzPlaceHolder]="'alert.notice.rule.priority.placeholder' | i18n"
            required
            name="priorities"
            id="priorities"
          >
            <nz-option [nzLabel]="'alert.priority.all' | i18n" [nzValue]="9"></nz-option>
            <nz-option [nzLabel]="'alert.priority.0' | i18n" [nzValue]="0"></nz-option>
            <nz-option [nzLabel]="'alert.priority.1' | i18n" [nzValue]="1"></nz-option>
            <nz-option [nzLabel]="'alert.priority.2' | i18n" [nzValue]="2"></nz-option>
          </nz-select>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="10" nzFor="repeat" nzRequired="true">{{ 'alert.converge.repeat' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="8">
          <nz-tag nzColor="processing" id="repeat" style="font-size: 14px">
            <i nz-icon nzType="filter" nzTheme="outline"></i>
            <span>{{ 'alert.converge.repeat-rule' | i18n }}</span>
          </nz-tag>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="10" nzFor="evalInterval" nzRequired="true">{{ 'alert.converge.eval-interval' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="8">
          <nz-input-number
            [(ngModel)]="converge.evalInterval"
            [required]="true"
            [nzMin]="0"
            [nzStep]="300"
            name="evalInterval"
            id="evalInterval"
          ></nz-input-number>
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label nzSpan="10" nzRequired="true" nzFor="enable">{{ 'alert.notice.rule.enable' | i18n }}</nz-form-label>
        <nz-form-control nzSpan="8">
          <nz-switch [(ngModel)]="converge.enable" name="enable" id="enable"></nz-switch>
        </nz-form-control>
      </nz-form-item>
    </form>
  </div>
</nz-modal>
